package org.jeeasy.generate.generator;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.NamingCase;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.Context;
import org.jeeasy.common.core.annotation.convert.BeanTransfer;
import org.jeeasy.common.core.base.convert.BaseConverter;
import org.jeeasy.common.core.base.convert.BaseTransfer;
import org.jeeasy.common.core.tools.Tools;
import org.jeeasy.generate.domain.GenTable;
import org.jeeasy.generate.domain.dto.GeneratorDTO;
import org.mapstruct.Mapper;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Objects;

@Data
@Slf4j
@EqualsAndHashCode(callSuper = true)
@BeanTransfer(value = TableContext.Converter.class)
public class TableContext extends BaseTransfer<GenTable> {
    private String name;
    private String summary;
    private String description;
    private String dataSource;
    private Integer izSync = 0;
    private Integer izSimpleQuery = 1;
    private Integer izPage = 1;
    private Integer izTree = 0;
    private String treePid = "pid";
    private String treeNameField = "name";
    private Integer formType = 12;
    private String tableType = "single";
    private String mainTable = "";
    private String slaveTable = "";
    private String relationType = "oneToMany";
    private String subTableStr;
    private Double tabOrderNum = 1.0;
    private String sysOrgCode;


    private CaseName caseName;
    private String date;
    private String author;
    private String apiPath;
    private String permission;

    private GeneratorDTO.GeneratorFile entity;
    private GeneratorDTO.GeneratorFile mapper;
    private GeneratorDTO.GeneratorFile mapperXml;
    private GeneratorDTO.GeneratorFile service;
    private GeneratorDTO.GeneratorFile serviceImpl;
    private GeneratorDTO.GeneratorFile controller;


    private List<FieldContext> fields;
    private List<IndexContext> indexes;

    public void putAuthorAndFiles(GenTable table, String author, List<GeneratorDTO.GeneratorFile> files) {
        this.caseName = new CaseName(this.name);
        this.date = Tools.getTime();
        this.author = StringUtils.defaultIfBlank(author, "wei.yang");

        this.indexes = table.getTableIndexes().stream().map(item -> item.transfer(IndexContext.class)).toList();
        this.fields = table.getTableFields().stream().map(item -> {
            FieldContext fieldContext = item.transfer(FieldContext.class);
            fieldContext.putField();
            return fieldContext;
        }).toList();

        for (GeneratorDTO.GeneratorFile file : files) {
            if (Objects.nonNull(BeanUtils.getPropertyDescriptor(this.getClass(), file.getType()))) {
                BeanUtil.setFieldValue(this, CaseName.toCamel(file.getType()), file);
            }
        }

        String moduleName = this.name.substring(0, this.name.indexOf("_"));
        String simpleName = NamingCase.toCamelCase(this.name.substring(this.name.indexOf("_") + 1));

        this.apiPath = String.format("/%s/%s", moduleName, simpleName);
        this.permission = String.format("%s-%s", moduleName, simpleName);
    }


    public Context toContext() {
        return new VelocityContext(BeanUtil.beanToMap(this));
    }

    @Mapper
    public interface Converter extends BaseConverter<GenTable, TableContext> {

    }

}
